import {
    createCondition,
    createLevelRatio,
    createRatioForTitle,
    createWeightPlan,
    deleteLevelRatio,
    deleteRatioForTitle,
    deleteWeightPlan,
    getConditionList,
    getLevelRatioList,
    getRatioForTitleList,
    getWeightPlanList,
    ncreateCondition,
    ngetConditionList,
    nupdateConditionValue,
    updateConditionValue,
    updateLevelRatio,
    updateRatioForTitle,
    updateWeightPlan,
} from 'api/index';
import { message } from 'antd';
import { pathToRegexp } from 'path-to-regexp';

/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2021/2/5 10:38 上午
 * @version V1.0
 */

export default {
    namespace: 'computeScheme',
    state: {
        tab1List: [],
        tab1Show: false,
        item1Data: {},

        tab2List: [],
        tab2Show: false,
        item2Data: {},

        tab3List: [],
        tab3Show: false,
        item3Data: {},

        tab4List: [],
        tab4Show: false,
        item4Data: {},


        tab5List: [],
        tab5Show: false,
        item5Data: {},
    },
    reducers: {
        setTab1List(state, { payload: tab1List }) {
            return { ...state, tab1List };
        },
        setTab1Show(state, { payload: tab1Show }) {
            return { ...state, tab1Show };
        },
        setItem1Data(state, { payload: item1Data }) {
            return { ...state, item1Data };
        },

        setTab2List(state, { payload: tab2List }) {
            return { ...state, tab2List };
        },
        setTab2Show(state, { payload: tab2Show }) {
            return { ...state, tab2Show };
        },
        setItem2Data(state, { payload: item2Data }) {
            return { ...state, item2Data };
        },


        setTab3List(state, { payload: tab3List }) {
            return { ...state, tab3List };
        },
        setTab3Show(state, { payload: tab3Show }) {
            return { ...state, tab3Show };
        },
        setItem3Data(state, { payload: item3Data }) {
            return { ...state, item3Data };
        },


        setTab4List(state, { payload: tab4List }) {
            return { ...state, tab4List };
        },
        setTab4Show(state, { payload: tab4Show }) {
            return { ...state, tab4Show };
        },
        setItem4Data(state, { payload: item4Data }) {
            return { ...state, item4Data };
        },


        setTab5List(state, { payload: tab5List }) {
            return { ...state, tab5List };
        },
        setTab5Show(state, { payload: tab5Show }) {
            return { ...state, tab5Show };
        },
        setItem5Data(state, { payload: item5Data }) {
            return { ...state, item5Data };
        },
    },
    effects: {
        * getTab1List({ payload }, { call, put, select }) {
            const result = yield call(getWeightPlanList, payload);
            if (result.status === 'success') {
                const tab1List = result.result;
                yield put({
                    type: 'setTab1List',
                    payload: tab1List,
                });
            } else {
                message.error(result.message);
            }
        },
        * addTab1Item({ payload, callback }, { call, put, select }) {
            const result = yield call(createWeightPlan, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        * updateTab1Item({ payload, callback }, { call, put, select }) {
            const result = yield call(updateWeightPlan, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        * deleteTab1Item({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteWeightPlan, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * getTab2List({ payload }, { call, put, select }) {
            const result = yield call(getLevelRatioList, payload);
            if (result.status === 'success') {
                const tab2List = result.result;
                yield put({
                    type: 'setTab2List',
                    payload: tab2List,
                });
            } else {
                message.error(result.message);
            }
        },

        * addTab2Item({ payload, callback }, { call, put, select }) {
            const result = yield call(createLevelRatio, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * updateTab2Item({ payload, callback }, { call, put, select }) {
            const result = yield call(updateLevelRatio, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * deleteTab2Item({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteLevelRatio, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },


        * getTab3List({ payload }, { call, put, select }) {
            const result = yield call(getRatioForTitleList, payload);
            if (result.status === 'success') {
                const tab3List = result.result;
                yield put({
                    type: 'setTab3List',
                    payload: tab3List,
                });
            } else {
                message.error(result.message);
            }
        },

        * addTab3Item({ payload, callback }, { call, put, select }) {
            const result = yield call(createRatioForTitle, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * updateTab3Item({ payload, callback }, { call, put, select }) {
            const result = yield call(updateRatioForTitle, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * deleteTab3Item({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteRatioForTitle, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },


        * getTab4List({ payload }, { call, put, select }) {
            const result = yield call(getConditionList, payload);
            if (result.status === 'success') {
                const tab4List = result.result;
                yield put({
                    type: 'setTab4List',
                    payload: tab4List,
                });
            } else {
                message.error(result.message);
            }
        },

        * addTab4Item({ payload, callback }, { call, put, select }) {
            const result = yield call(createCondition, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * updateTab4Item({ payload, callback }, { call, put, select }) {
            const result = yield call(updateConditionValue, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },


        * getTab5List({ payload }, { call, put, select }) {
            const result = yield call(ngetConditionList, payload);
            if (result.status === 'success') {
                const tab5List = result.result;
                yield put({
                    type: 'setTab5List',
                    payload: tab5List,
                });
            } else {
                message.error(result.message);
            }
        },

        * addTab5Item({ payload, callback }, { call, put, select }) {
            const result = yield call(ncreateCondition, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * updateTab5Item({ payload, callback }, { call, put, select }) {
            const result = yield call(nupdateConditionValue, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp('/teaching-quality/compute-scheme').exec(pathname)) {

                }
            });
        },
    },
};
